package com.quanyan.place.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import com.quanyan.common.exception.BizException;
import com.quanyan.place.constants.PlaceConstants;
import com.quanyan.place.entity.TbBizPlaceUnit;
import com.quanyan.place.entity.TbBizPlaceUnitExample;
import com.quanyan.place.entity.TbBizPlaceUnitSalePlan;
import com.quanyan.place.entity.vo.TbBizPlaceUnitVo;
import com.quanyan.place.mapper.PlaceUnitSalePlanMapper;
import com.quanyan.place.mapper.TbBizPlaceUnitMapper;
import com.quanyan.place.service.PlaceLampControlService;

/**
 * Created by songfan on 2017/5/12.
 */

@Service("placeLampControlService")
public class PlaceLampControlServiceImpl implements PlaceLampControlService {

	@Autowired
	private TbBizPlaceUnitMapper tbBizPlaceUnitMapper;
	@Autowired
	private PlaceUnitSalePlanMapper placeUnitSalePlanMapper;
	
	@Override
	public List<TbBizPlaceUnitVo> queryPlaceUnitByCagetory(TbBizPlaceUnitVo tbBizPlaceUnitVo) {
		
		Assert.notNull(tbBizPlaceUnitVo.getPlaceId(), "场馆ID不能为空！");
		Assert.notNull(tbBizPlaceUnitVo.getCategoryId(), "运动类型ID不能为空！");
		
		TbBizPlaceUnitExample tbBizPlaceUnitExample = new TbBizPlaceUnitExample();
		tbBizPlaceUnitExample.createCriteria().andPlaceIdEqualTo(tbBizPlaceUnitVo.getPlaceId())
		.andCategoryIdEqualTo(tbBizPlaceUnitVo.getCategoryId()).andIsSignEqualTo(PlaceConstants.IS_SIGN_YES)
		.andIsDeleteEqualTo((byte)0);//0表示未删除
		
		List<TbBizPlaceUnit> tbBizPlaceUnitList =  tbBizPlaceUnitMapper.selectByExample(tbBizPlaceUnitExample);
		if (!CollectionUtils.isEmpty(tbBizPlaceUnitList)) {
			List<TbBizPlaceUnitVo> tbBizPlaceUnitVoList = new ArrayList<TbBizPlaceUnitVo>();
			
			for (TbBizPlaceUnit tbBizPlaceUnit : tbBizPlaceUnitList) {
				TbBizPlaceUnitVo tbBizPlaceUnitVoTemp = new TbBizPlaceUnitVo();
				BeanUtils.copyProperties(tbBizPlaceUnit, tbBizPlaceUnitVoTemp);
				tbBizPlaceUnitVoList.add(tbBizPlaceUnitVoTemp);
			}
			return tbBizPlaceUnitVoList;
		}
		return null;
	}

	@Override
	public void checkPlaceUint(TbBizPlaceUnitVo tbBizPlaceUnitVo) {
		String[] placeUintIdList = tbBizPlaceUnitVo.getPlaceUintId().split(",");
		//做关灯校验，当前单元存在订场，则提示操作用户是否关灯
		for (String placeUintId : placeUintIdList) {
			TbBizPlaceUnitSalePlan tbBizPlaceUnitSalePlan  = placeUnitSalePlanMapper.queryUnitSalePlan(Integer.valueOf(placeUintId));
			if(tbBizPlaceUnitSalePlan != null 
					&& !PlaceConstants.PLACE_PREORDER_STATUS.equals(tbBizPlaceUnitSalePlan.getStatus())){
				throw new BizException("当前"+tbBizPlaceUnitSalePlan.getPlaceUnitName()+"有客户正在打球,不能关灯");
			}
		}
	}

}
